package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysNodeBo;
import com.xmtlxx.system.domain.vo.SysNodeVo;
import com.xmtlxx.system.service.ISysNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 工作流节点Controller
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Validated
@Api(value = "工作流节点控制器", tags = {"工作流节点管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysNode")
public class SysNodeController extends BaseController {

    private final ISysNodeService iSysNodeService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysNodeVo> initBean(){
        SysNodeVo vo = new SysNodeVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询工作流节点列表
     */
    @ApiOperation("查询工作流节点列表")
    @SaCheckPermission("system:sysNode:list")
    @GetMapping("/list")
    public R<List<SysNodeVo>> list(@Validated(QueryGroup.class) SysNodeBo bo) {
        List<SysNodeVo> list = iSysNodeService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出工作流节点列表
     */
    @ApiOperation("导出工作流节点列表")
    @SaCheckPermission("system:sysNode:export")
    @Log(title = "工作流节点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysNodeBo bo, HttpServletResponse response) {
        List<SysNodeVo> list = iSysNodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "工作流节点", SysNodeVo.class, response);
    }

    /**
     * 获取工作流节点详细信息
     */
    @ApiOperation("获取工作流节点详细信息")
    @SaCheckPermission("system:sysNode:query")
    @GetMapping("/{nid}")
    public R<SysNodeVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("nid") String nid) {
        return R.ok(iSysNodeService.queryById(nid));
    }

    /**
     * 新增工作流节点
     */
    @ApiOperation("新增工作流节点")
    @SaCheckPermission("system:sysNode:add")
    @Log(title = "工作流节点", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysNodeVo vo) {
        SysNodeBo bo = new SysNodeBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysNodeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改工作流节点
     */
    @ApiOperation("修改工作流节点")
    @SaCheckPermission("system:sysNode:edit")
    @Log(title = "工作流节点", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysNodeVo vo) {
        SysNodeBo bo = new SysNodeBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysNodeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除工作流节点
     */
    @ApiOperation("删除工作流节点")
    @SaCheckPermission("system:sysNode:remove")
    @Log(title = "工作流节点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{nids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] nids) {
        return toAjax(iSysNodeService.deleteWithValidByIds(Arrays.asList(nids), true) ? 1 : 0);
    }
}
